function updateLoad(){
    var params = $$.getQueryString();
    $$.ajax({
        url:"/rest/IotPointBusiness/query",
        data : {
            id : params.rowId
        },
        success : function(data){
            $("#b_id").val(data.id);
            $("#b_deviceId").val(data.deviceId);
            $("#b_deviceCode").val(data.deviceCode);
            $("#b_dataCode").val(data.dataCode);
            $("#b_dataName").val(data.dataName);

            $("#b_dataType").val(data.dataType);
            $("#b_dataRange").val(data.dataRange);
            $("#b_dataExpr").val(data.dataExpr);
            $("#b_dataScale").val(data.dataScale);
            $("#b_dataUnit").val(data.dataUnit);
            $("#b_dataCycle").val(data.dataCycle);
            $("#b_dataYield").val(data.dataYield);
            $("#b_dataMultiple").val(data.dataMultiple);
            $("#b_dataLj").val(data.dataLj);
            $("#b_dataZero").val(data.dataZero);
            $("#b_dataEdata").val(data.dataEdata);
            $("#b_rowSort").val(data.rowSort);

            $("#b_statusOld").val(data.status);
            $("#b_dataCodeOld").val(data.dataCode);


            var status=data.status;
            $("input[name^='b_status_']").each(function(){
                if($(this).val()==status){
                    this.checked='checked';
                }

            });
            $("input[name^='b_dataLj_']").each(function(){
                if($(this).val()==status){
                    this.checked='checked';
                }

            });
        }
    });



}
function addData(){
    var param={};
    $$.form({
        checkForm : function(){
            return $("#theform").valid();
        },
        success : function(){
            $$.alert("成功！");
            returnMain();
        }
    });

}
function returnMain(){
    $$.goUrl("/iot/points/points_list.html");
}
$(function () {
    collectionFormValid();
    $("input[name^='b_status']").click(function(){
        $("#b_status").val($(this).val());
    });
    $("input[name^='b_dataLj']").click(function(){
        $("#b_dataLj").val($(this).val());
    });


    $("#subBtn").click(function(){
        addData();
    });
    $("#returnBut").click(function(){
        returnMain();
    });
    $("#b_deviceCode").on('click',function () {
        layer.open({
            move: false,
            type: 2,
            title: '选择设备编码',
            area: ['900px', '500px'],
            content: 'select_device_list.html',
            btn: ['确定','取消'],
            btnAlign: 'c',
            yes: function (index, layero) {
                var openWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                var ret= openWin.yesOperator();
                console.log(JSON.stringify(ret));
                   //alert(JSON.stringify(ret));
                    $("#b_deviceCode").val(ret.deviceCode);
                    $("#b_deviceId").val(ret.id);
                    layer.close(index);
            }, cancel: function (index) {
                layer.close(index);
            }
        })
    });






})
function collectionFormValid() {
    $("#theform").validate({
        rules: {
            b_deviceCode: {
                required: true,
            },
            b_dataType: {
                required: true,
            },
            b_dataCode: {
                required: true,
                minlength: 3,
                isCode:true
            },
            b_dataRange:{
                required: true,
                minlength: 1,
                isRange: true,
            },
            b_dataScale:{
                required: true,
                range:[0,5]
            },
            b_dataCycle:{
                digits:true
            },
            b_dataYield:{
                minlength: 1,
                isYield: true,
            },
            b_dataMultiple:{
                digits:true
            },
            b_rowSort: {
                required: true,
                minlength: 1,
                isPositiveInteger: true
            }
        },
        errorPlacement: function(error, element) {
            error.appendTo(element.parent());
        },
        errorElement: "span",
        errorClass: "errorDevice",
        success: 'valid',
        messages: {
            b_deviceCode: {
                required: " (必填)",
            },
            b_dataType: {
                required: " (必填)",
            },
            b_dataCode:{
                required: " (必填)",
                minlength: " (不能少于 3 个字母)",

            },
            b_dataRange:{
                required: " (必填)",
                minlength: "(不能少于1个数字)",
            },
            b_dataScale:{
                required: " (必填)",
                range:"输入0-5数字"
            },
            b_dataCycle:{
                digits:"输入正整数"
            },
            b_dataYield:{
                minlength: "(不能少于1个数字)",
            },
            b_dataMultiple:{
                digits:"输入正整数"
            },
            b_rowSort:{
                isDigits: "输入正整数"
            }
        },
    });
}
jQuery.validator.addMethod("isCode", function(value, element) {
    //return this.optional(element) || /^[A-Z]{3}[0-9]{4}$/.test(value);
    if(!/^[A-Z]{3}[0-9]{4}$/.test(value)){
        $(element).data('error-msg','请输入3位大写字母＋4位数字');
        return false;
    }
    //校验编码是否存在
    var fieldName="dataCode";
    var fieldNameCn="数据编码";
    var serviceName="IotPointBusiness";
    var Code=$("#b_"+fieldName).val();
    var CodeOld=$("#b_"+fieldName+"Old").val();
    if(Code==CodeOld){
      return true;
    }else{

        var args={};
        args.c_dataCode_1=$("#b_"+fieldName+"").val();
        args.page=0;
        args.pageSize=5;
        args.isPage=1;


        var exists;
        $$.ajax({
            url: "/bootstrap/basegrid/"+serviceName+"/list",
            async:false,
            data: args,
            success: function (data) {
                if(data.length && data.length>0){
                    exists=true;
                }else{
                    exists=false;
                }
            }
        });
        if(exists){
            $(element).data('error-msg','编码不能重复！');
            return false;
        }
        return true;
    }

}, function(params, element) {
    return $(element).data('error-msg');
});
jQuery.validator.addMethod("isRange", function(value, element) {
    return this.optional(element) || /^(([0-9]+)|([0-9]+\.[0-9]{0,2}))$/.test(value);
},'只能包含数字和小数点，小数点保留两位');
jQuery.validator.addMethod("isYield", function(value, element) {
    return this.optional(element) || /^(([0-9]+)|([0-9]+\.[0-9]{0,3}))$/.test(value);
},'只能包含数字和小数点，小数点保留三位');